package david.java.flink_sql.udf.offline;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

/**
 * @Description: 将两个字段用","切割成数组, 排序后用","拼接, 合并成一个字符串
 * @Author: ZhaoDawei
 * @Date: Create in 5:31 下午 2022/6/28
 */
public class MergeString extends UDF {

    public String evaluate(String str1, String str2) {
        return merge(",", str1, str2);
    }


    private String merge(String seperator,String str1, String str2) {
        if (StringUtils.isBlank(str1) && StringUtils.isBlank(str2)) {
            return null;
        }

        HashSet<String> set = new HashSet<>();
        if (StringUtils.isNotBlank(str1)) {
            set.addAll(Arrays.asList(str1.split(seperator)));
        }
        if (StringUtils.isNotBlank(str2)) {
            set.addAll(Arrays.asList(str2.split(seperator)));
        }

        ArrayList<String> list = new ArrayList<>(set);
        Collections.sort(list);

        return String.join(seperator, list);
    }

    public static void main(String[] args) {
        String s1 = "a,b,c,d";
        String s2 = "f,b,e,d";

        MergeString mergeString = new MergeString();
        System.out.println(mergeString.evaluate(s1, s2));
    }

}
